package com.example.wheresmystuff2;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Class that provides the back-end support for activity_add_lost_item.xml (The 'Add Item' page)
 */
public class AddItem extends Activity {

	Database db;
	
	/**
	 * Method that is called when activity is created and sets up the global state
	 * @param savedInstanceState the last state saved on last running this app (saved by onSaveInstanceState(
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		db = new Database(this);
		db.open();
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_item);
		
		//create button instance and save the 'Add' button's id in it
		Button doneButton = (Button) findViewById(R.id.add_item_done);
		Button cancelButton = (Button) findViewById(R.id.add_item_cancel);
		
		//Create radio groups
		final RadioGroup rg1 = (RadioGroup)findViewById(R.id.lost_or_found_radio_group);
		final RadioGroup rg2 = (RadioGroup)findViewById(R.id.category_radio_group);
		
		//create instances of editText for each text form line
		final EditText nameField = (EditText) findViewById(R.id.lost_item_name);
		final EditText descriptionField = (EditText) findViewById(R.id.lost_item_description);
		final EditText locationField = (EditText) findViewById(R.id.lost_item_location);
		final EditText rewardField = (EditText) findViewById(R.id.lost_item_reward);
		
		//error field to be updated if error
		final TextView error= (TextView) findViewById(R.id.add_item_errorField);
		
		//add button listener to 'Done' button
		doneButton.setOnClickListener(new OnClickListener()
	    {
			/**
			 * Method that sends data from buttons and text fields to Gallery when 'Done' button is pressed
			 * @param v the view being pressed ('Done' button)
			 */
			public void onClick(View v) {
				//radio buttons that were selected when button pressed
				final RadioButton lostOrFoundButton = (RadioButton) findViewById(rg1.getCheckedRadioButtonId());
				final RadioButton categoryButton = (RadioButton) findViewById(rg2.getCheckedRadioButtonId());
				
				//instantiate strings for adding to gallery
				String type = lostOrFoundButton.getText().toString(); //lost or found
				String category = categoryButton.getText().toString();//Keepsake, Heirloom, or Misc.
				String name = nameField.getText().toString();
				String description = descriptionField.getText().toString();
				String location = locationField.getText().toString();
				String reward = rewardField.getText().toString();			
				
				//check if fields have at least 3 characters before adding
				if(!(name.equals("") || description.equals("") || location.equals("") || reward.equals(""))){
					
					if(!(name.length()<3 || description.length()<3 || location.length()<3 || reward.length()<3 )){
						
						SimpleDateFormat formatter = new SimpleDateFormat("MM'/'dd'/'yyyy");
						Date date = new Date();
						String formattedDate = formatter.format(date);						
						
						db.addItemRow(name, description, location, "Open", category, type, formattedDate, reward);
						
						//Make toasts to indicate when items are added
						Toast.makeText(getApplicationContext(), "Item added", Toast.LENGTH_SHORT).show();
					 	Intent myIntent = new Intent(AddItem.this, Gallery.class);
					 	AddItem.this.startActivity(myIntent);
					}
					
					else {
						error.setText("All fields must have >3 characters.");
					}
		    	}
				else{
		    		 error.setText("All fields are required.");
		    	}
			}
	    });
		
		cancelButton.setOnClickListener(new OnClickListener()
	    {
			/**
			 * Method that sends data from buttons and text fields to Gallery when 'Cancel' button is pressed
			 * @param v the view being pressed ('Cancel' button)
			 */
			public void onClick(View v) {				
			 	Intent myIntent = new Intent(AddItem.this, Gallery.class);
			 	AddItem.this.startActivity(myIntent);	
			}
	    });
	}

	/**
	 * Initialize AddLostItem's standard options menu
	 * @param menu the options menu
	 * @return true for menu displayed, false if not displayed
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.add_lost_item, menu);
		return true;
	}
}
